'use strict';
const {parseTime, parseNum} = require("../utils");

module.exports = app => {
    const { STRING, INTEGER, DATE, TINYINT, DECIMAL } = app.Sequelize;
    const Order = app.model.define(
        'Order',
        {
            id: {type: INTEGER, primaryKey: true, autoIncrement: true},
            order_no: STRING(255),
            user_id: INTEGER,
            address_id: {type: INTEGER, defaultValue: 0},
            address_detail: STRING(255),
            address_nickname: STRING(255),
            address_phone: STRING(11),
            remark: STRING(255),
            status: {
                type: STRING,
                defaultValue: 'pending',
                /**
                 *  'pending' COMMENT '待支付：订单已创建，但尚未完成支付',
                 *         'paid' COMMENT '已支付：订单已完成支付',
                 *         'refunding' COMMENT '退款中：订单正在进行退款流程',
                 *         'shipped' COMMENT '已发货：订单商品已发出',
                 *         'refunded' COMMENT '已退款：订单退款流程已完成',
                 *         'completed' COMMENT '已完成：订单所有流程（支付、发货等）均已完成',
                 *         'cancelled' COMMENT '已取消：订单已被取消'
                 *         'pending_receipt' COMMENT '待收货': 订单已完成支付，但尚未完成收货
                 */
            },
            order_delivery: {
                type: STRING,
                // express 快递, self_pickup 自提, no_delivery 虚拟
                defaultValue: 'express',
            },
            // 快递单号
            express_no: STRING(255),
            // 快递公司
            express_company: STRING(255),
            time_expire: {
                type: DATE,
                get() {
                    return parseTime(this.getDataValue('time_expire'));
                }
            },
            count: {type: INTEGER, defaultValue: 0},
            amount: {
                type: DECIMAL(10, 2),
                defaultValue: 0.00,
                get() {
                    return parseNum(this.getDataValue('amount'));
                }
            },
            payment_time: {
                type: DATE,
                get() {
                    return parseTime(this.getDataValue('payment_time'));
                }
            },
            payment_type: STRING(50),
            payment_amount: {
                type: DECIMAL(10, 2),
                defaultValue: 0.00,
                get() {
                    return parseNum(this.getDataValue('payment_amount'));
                }
            },
            discount_amount: {
                type: DECIMAL(10, 2),
                defaultValue: 0.00,
                get() {
                    return parseNum(this.getDataValue('discount_amount'));
                }
            },
            discount_coupon_id: INTEGER,
            // 配送费用配置id
            delivery_fee_config_id: INTEGER,
            // 配送费
            delivery_amount: {
                type: DECIMAL(10, 2),
                defaultValue: 0.00,
                get() {
                    return parseNum(this.getDataValue('delivery_amount'));
                }
            },
            transaction_id: STRING(255),
            prepay_id: STRING(255),
            refund_no: STRING(255),
            refund_transaction_id: STRING(255),
            refund_status: {type: TINYINT, defaultValue: -1},
            refund_time: {
                type: DATE,
                get() {
                    return parseTime(this.getDataValue('refund_time'));
                }
            },
            refund_success_time: {
                type: DATE,
                get() {
                    return parseTime(this.getDataValue('refund_success_time'));
                }
            },
            refund_reason: STRING(255),
            refund_remark: STRING(255),
            refund_amount: {type: DECIMAL(10, 2), defaultValue: 0.00},
            cancel_time: {
                type: DATE,
                get() {
                    return parseTime(this.getDataValue('cancel_time'));
                }
            },
            create_time: {
                type: DATE,
                get() {
                    return parseTime(this.getDataValue('create_time'));
                }
            },
            update_time: {
                type: DATE,
                get() {
                    return parseTime(this.getDataValue('update_time'));
                }
            }
        },
        {
            tableName: 'order',
            timestamps: true,
            createdAt: 'create_time',
            updatedAt: 'update_time'
        }
    );

    Order.associate = function() {
        app.model.Order.belongsTo(app.model.User, {as: 'user', foreignKey: 'user_id'});
        app.model.Order.hasMany(app.model.OrderItem, {as: 'items', foreignKey: 'order_id'});
        app.model.Order.belongsTo(app.model.Address, {as: 'address', foreignKey: 'address_id'});
        // 评价
        app.model.Order.hasMany(app.model.Comment, {as: 'comments', foreignKey: 'order_id'});
    }
    return Order
};
